<?php

final class DkLabCaptcha
{
    
    private $_keystring;
    
    // returns keystring
    public function getKeyString() 
    {
        return $this->_keystring;
    }

    // sets keystring
    public function setKeyString($value)
    {
        $this->_keystring = $value;
    }
    
    private function _defineKeyString() 
    {
        for ($i = 0; $i < 5; $i ++) {
            $this->_keystring .= mt_rand(0, 9);
        }
    }
    
    public function output() 
    {
        $im = imagecreatefrompng(dirname(__FILE__) . '/bottom_layers/dk_lab_captcha.png'); 
        $shadow = imagecolorallocate($im, 0, 0, 0);
        $color = imagecolorallocate($im, 118, 227, 139);
        $font = dirname(__FILE__) . '/fonts/times.ttf';
        
        if (is_null($this->_keystring)) {
            $this->_defineKeyString();
        }
        
        for ($i=0; $i < strlen($this->_keystring); $i ++) {
            imagettftext($im, 35, $i*-5, 22+$i*30, 25+$i*15, $shadow, $font, $this->_keystring[$i]);
            imagettftext($im, 35, $i*-5, 20+$i*30, 27+$i*15, $color, $font, $this->_keystring[$i]);
        }
        
        header("Content-Type: image/x-png");
        imagepng($im);
    }
    
}